package bingo.validation;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 表示一个定义在Class上的所有约束的集合 
 */
public class ClassChecks {
	
	private Class<?> type;
	
	private ArrayList<Checker> checkers = new ArrayList<Checker>();
	
	private Map<Field,FieldChecks> fieldChecks = new HashMap<Field, FieldChecks>();
	
	public ClassChecks(Class<?> type) {
		this.type = type;
	}
	
	public Class<?> getType(){
		return this.type;
	}
	
	public void addTypeChecker(Checker... checkers){
		for(int i=0; i<checkers.length; i++){
			this.checkers.add(checkers[i]);
		}
	}
	
	public void addFieldCheckers(Field field,Checker... checkers){
		FieldChecks checks = fieldChecks.get(field);
		
		if(null == checks){
			checks = new FieldChecks(field);
		}
		
		checks.addCheckers(checkers);
		fieldChecks.put(field, checks);
	}
	
	public List<Checker> getTypeCheckers(){
		return this.checkers;
	}
	
	public FieldChecks getFieldChecks(Field field){
		return fieldChecks.get(field);
	}
	
	public static final class FieldChecks{
		
		private Field field;
		
		private List<Checker> checkers = new ArrayList<Checker>();
		
		public FieldChecks(Field field){
			this.field = field;
		}
		
		public void addCheckers(Checker... checkers){
			for(int i=0; i<checkers.length; i++){
				this.checkers.add(checkers[i]);
			}
		}
		
		public List<Checker> getCheckers(){
			return this.checkers;
		}
		
		public Field getField(){
			return this.field;
		}
	}
}
